import json

from Devices.Andlink.AndlinkParser import AndlinkParser

class AndlinkWaterleakParser(AndlinkParser):
    def ParseQuery(data, deviceQueryInfo):
        doc = json.loads(data)
        if doc is None:
            return False

        if not "data" in doc:
            return False

        dataObj = doc["data"]

        return AndlinkWaterleakParser.ParseData(dataObj, deviceQueryInfo)

    def ParseData(dataObj, deviceQueryInfo):
        if not "params" in dataObj:
            return False

        params = dataObj["params"]
        for i in range(len(params)):
            param = params[i]
            ret, paramCode = AndlinkParser._GetStrProperty(param, "paramCode")
            if not ret:
                return False

            if "waterLeak" == paramCode:
                ret, paramValue = AndlinkParser._GetIntProperty(param, "paramValue")
                if not ret:
                    return False
                deviceQueryInfo.waterLeak = paramValue
            elif "voltageStatus" == paramCode:
                ret, paramValue = AndlinkParser._GetIntProperty(param, "paramValue")
                if not ret:
                    return False
                deviceQueryInfo.voltageStatus = paramValue
            elif "protectionStatus" == paramCode:
                ret, paramValue = AndlinkParser._GetIntProperty(param, "paramValue")
                if not ret:
                    return False
                deviceQueryInfo.protectionStatus = paramValue
            elif "voltage" == paramCode:
                ret, paramValue = AndlinkParser._GetIntProperty(param, "paramValue")
                if not ret:
                    return False
                deviceQueryInfo.voltage = paramValue

        return True
    
    def PackWriteSN(sn):
        doc = {}
        doc["command"] = 1000

        data = {}
        data["type"] = 0
        data["sn"] = sn

        doc["data"] = data

        return json.dumps(doc, separators=(',', ':'))

    def PackWriteCMEI(cmei):
        doc = {}
        doc["command"] = 1000

        data = {}
        data["type"] = 0
        data["cmei"] = cmei

        doc["data"] = data

        return json.dumps(doc, separators=(',', ':'))

    def PackWriteRstMode(rstMode):
        doc = {}
        doc["command"] = 1000

        data = {}
        data["type"] = 0
        data["mode"] = rstMode

        doc["data"] = data

        return json.dumps(doc, separators=(',', ':'))

    def PackQuickSleep():
        doc = {}
        doc["command"] = 1000

        data = {}
        data["type"] = 1

        doc["data"] = data

        return json.dumps(doc, separators=(',', ':'))

    def PackSleepEnable(enable):
        doc = {}
        doc["command"] = 1000

        data = {}
        data["type"] = 2
        data["enable"] = int(enable)

        doc["data"] = data

        return json.dumps(doc, separators=(',', ':'))



